package com.yuedou.api.controller.user;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yuedou.api.constant.ResponseCode;
import com.yuedou.api.controller.platform.BaseController;
import com.yuedou.api.entity.mer.Mer;
import com.yuedou.api.entity.mer.MerUser;
import com.yuedou.api.entity.platform.Parameter;
import com.yuedou.api.entity.platform.SplitAccount;
import com.yuedou.api.entity.platform.SplitAccountRecord;
import com.yuedou.api.entity.wxuser.User;
import com.yuedou.api.service.mer.MerService;
import com.yuedou.api.service.mer.MerUserService;
import com.yuedou.api.service.platfom.ParameterService;
import com.yuedou.api.service.platfom.PlatformPayService;
import com.yuedou.api.service.platfom.SplitAccountRecordService;
import com.yuedou.api.service.platfom.SplitAccountService;
import com.yuedou.api.service.wxuser.UserService;
import com.yuedou.api.util.GsonResultBean;
import com.yuedou.api.util.Tools;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: ZhouWei
 *@Description:  账户
 *@Date 2025-01-17
 */
@Component
@RestController
@RequestMapping("/account")//url:模块/资源/{}/细分
public class SplitAccountController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private SplitAccountService splitAccountService;
    @Resource
    private SplitAccountRecordService splitAccountRecordService;
    @Resource
    private MerService merService;
    @Resource
    private MerUserService merUserService;
    @Resource
    private ParameterService parameterService;


    /**
     *  账户信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/test")
    public Object test(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int account_id = jsonRequest.containsKey("account_id") ? jsonRequest.getInt("account_id") : 0;

//            User user = userService.findById(user_id);
//            if(user == null){
//                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
//            }

            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功"),true,true);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  账户信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/detail")
    public Object add(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int is_person = jsonRequest.containsKey("is_person") ? jsonRequest.getInt("is_person") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            List<SplitAccount> list = splitAccountService.getAccountByUserId(user_id,is_person);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",list.size(),list),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  账户信息(兵哥)
     * @param
     * @throws Exception
     */
    @PostMapping(value="/detail/bingge")
    public Object detailBingge(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String, Object> map = new HashMap<>();
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int is_person = jsonRequest.containsKey("is_person") ? jsonRequest.getInt("is_person") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            List<SplitAccount> list = splitAccountService.getAccountByUserId(user_id,is_person);
            map.put("accountList", list);

            Parameter parameter = parameterService.getParameterById(1);
            if(parameter != null){
                map.put("is_open_cash_bingge",parameter.getIsOpenCashBingge());
            }

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",list.size(), map),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  我的个人账户信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/my/personal/detail")
    public Object myAccountDetail(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            SplitAccount account = splitAccountService.getAccountByUserId(user_id,4, user_id);
            Map<String,Object> map = new HashMap<>();
            map.put("account",account);


            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",map),true,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  账户记录信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/record/list")
    public Object recordList(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 20;
            Integer from_type = jsonRequest.containsKey("from_type") ? jsonRequest.getInt("from_type") : null;
            int account_id = jsonRequest.containsKey("account_id") ? jsonRequest.getInt("account_id") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            List<SplitAccountRecord> list = splitAccountRecordService.getPageByUserId(user_id,account_id,from_type,pageIndex,pageSize);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",list.size(),list),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


    /**
     *  账户记录信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/record/list/bingge")
    public Object recordListBingge(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 20;
            Integer from_type = jsonRequest.containsKey("from_type") ? jsonRequest.getInt("from_type") : null;
            int account_id = jsonRequest.containsKey("account_id") ? jsonRequest.getInt("account_id") : 0;
            int in_or_out = jsonRequest.containsKey("in_or_out") ? jsonRequest.getInt("in_or_out") : 0;// 1 - 收入 0 - 提现

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            List<SplitAccountRecord> list = splitAccountRecordService.getPageByUserId(user_id,account_id,from_type,in_or_out,pageIndex,pageSize);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",list.size(),list),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }

    /**
     *  账户记录信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/getAmountByMerId")
    public Object accountList(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int mer_id = jsonRequest.containsKey("mer_id") ? jsonRequest.getInt("mer_id") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            Mer region = merService.findById(mer_id);
            if(region == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该商户不存在"),true,true);
            }

            MerUser bindingUser = merUserService.getMerByUser(user_id,mer_id);
            if(bindingUser == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"您没有该权限"),true,true);
            }

            Map<String,Object> map = new HashMap<>();
            //账户类型  0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
            SplitAccount splitAccount = splitAccountService.getAdminAccountByType(mer_id,3);
            if(splitAccount != null){
                map.put("split_money",splitAccount.getSplitMoney());
            }
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",map),true,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }

    /**
     *  银行卡号设置
     * @param
     * @throws Exception
     */
    @PostMapping(value="/setBankInfo")
    public Object setBankInfo(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int account_id = jsonRequest.containsKey("account_id") ? jsonRequest.getInt("account_id") : 0;//split_money 表的id
            String bankName = jsonRequest.containsKey("bank_name") ? jsonRequest.getStr("bank_name") : "";
            String id_no = jsonRequest.containsKey("id_no") ? jsonRequest.getStr("id_no") : "";
            String cardNum = jsonRequest.containsKey("card_num") ? jsonRequest.getStr("card_num") : "";
            String realName = jsonRequest.containsKey("real_name") ? jsonRequest.getStr("real_name") : "";


            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }

            SplitAccount splitAccount = splitAccountService.findById(account_id);
            if(splitAccount == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该账户不存在"),true,true);
            }

            if(splitAccount.getUserId().intValue() != user_id){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"暂无该权限"),true,true);
            }

            splitAccount.setBankName(bankName);
            splitAccount.setIdNo(id_no);
            splitAccount.setCardNum(cardNum);
            splitAccount.setRealName(realName);

            boolean ret = splitAccountService.updateById(splitAccount);
            if(ret){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"修改成功"),true,true);
            }else{
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"修改失败"),true,true);
            }
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }


}
